import axios from 'axios'
import {Message} from 'element-ui'

// axios实例
const service = axios.create({
  baseURL: 'http://localhost',
  timeout: 5000
})

service.interceptors.request.use(
  config => {
    return config
  },
  err => {
    return Promise.reject(err)
  }
)

service.interceptors.response.use(
  response => {
    if (response.data.code !== 200) {
      Message({
        message: response.data.message,
        type: 'error',
        duration: 5 * 1000
      })

      return Promise.reject(response.data)
    }else {
      return response.data
    }
  },
  error => {
    return Promise.reject(error.response)
  }
)

export default service
